package web.admin.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.TherapeuticArea;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CriteriaTherapeuticArea;
import services.TherapeuticAreaCRUDService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 23/08/12
 * Time: 16:54
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Admin/TherapeuticArea")
public class TherapeuticAreaController extends ActionSupport implements ModelDriven {

    private TherapeuticAreaCRUDService therapeuticAreaCRUDService;
    private TherapeuticArea TherapeuticArea = new TherapeuticArea();
    private List<TherapeuticArea> list;
    private Integer therapeuticAreaId;

    @Autowired
    public TherapeuticAreaController(TherapeuticAreaCRUDService therapeuticAreaCRUDService){
        this.therapeuticAreaCRUDService = therapeuticAreaCRUDService;
    }

    @Override
    public Object getModel() {
        return TherapeuticArea;
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.TherapeuticArea.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<domain.entities.TherapeuticArea>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.TherapeuticArea.tiles", type="tiles")
    })
    public String list() {

        CriteriaTherapeuticArea crta = new CriteriaTherapeuticArea(TherapeuticArea);
        list = therapeuticAreaCRUDService.list(crta);
        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.TherapeuticArea.create.tiles", type="tiles")
    })
    public String createTherapeuticArea() {

        if(therapeuticAreaId != null){
            TherapeuticArea = therapeuticAreaCRUDService.findById(therapeuticAreaId);

        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveTherapeuticArea() {
        therapeuticAreaCRUDService.save(TherapeuticArea);
        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String deleteTherapeuticArea() {

        therapeuticAreaCRUDService.delete(TherapeuticArea);

        return SUCCESS;
    }


    public Integer getTherapeuticAreaId() {
        return therapeuticAreaId;
    }

    public void setTherapeuticAreaId(Integer TherapeuticAreaId) {
        this.therapeuticAreaId = TherapeuticAreaId;
    }

    public TherapeuticArea getTherapeuticArea() {
        return TherapeuticArea;
    }

    public void setTherapeuticArea(TherapeuticArea TherapeuticArea) {
        this.TherapeuticArea = TherapeuticArea;
    }

    public List<TherapeuticArea> getList() {
        return list;
    }

    public void setList(List<TherapeuticArea> list) {
        this.list = list;
    }
}
